<?php

namespace app\selladmin\model;

use think\Model;
use think\Db;

/*
 * 订单
 */

class Order extends Model {

    const ORDTYPE_1 = 1; //现金
    const ORDTYPE_2 = 2; //卡支付
    const ORDTYPE_3 = 3; //积分支付

    //订单对应的用户

    public function Custom() {
        return $this->hasOne('Custom', 'id', 'ctm_id')->field('id,custom_name,real_name,custom_phone');
    }

    //订单对应的代金券
    public function Coupon() {
        return $this->hasOne('Coupon', 'id', 'cun_id')->field('id,money,reach');
    }

    //订单对应的会员卡
    public function Vipcard() {
        return $this->hasOne('Vipcard', 'id', 'vip_id')->field('id,vip_num,vip_name,vip_lines,vip_discount');
    }

    //查询订单信息
    public function OrderInfo($store_id, $ord_num, $ctm_id, $pagenum) {
        if ($ord_num) {
            $orderwhere['ord_num'] = $ord_num;
        }
        if ($store_id) {
            $orderwhere['store_id'] = $store_id;
        }
        if ($ctm_id) {
            $orderwhere['ctm_id'] = $ctm_id;
        }
        $orderwhere = isset($orderwhere) ? $orderwhere : [];
        $orderList = Order::with('Custom')
                ->where($orderwhere)
                ->field("id,ord_num,ctm_id,pay_money,status,add_time,pay_time,store_id")
                ->order("add_time desc")
                ->paginate($pagenum, false, ['var_page' => 'page1', 'fragment' => 'order'])
                ->appends(['store_id' => $store_id, 'id' => $store_id, 'ord_num' => $ord_num]);
        return $orderList;
    }

    //查询订单详情
    public function OrderDetails($ordid) {
        $OrderDetails = Order::with('Custom,Coupon,Vipcard')
                ->where("id=" . $ordid)
                ->find();
        $itemdata = OrdPro::where("ord_id=" . $OrderDetails['id'])->field("pro_id,number,type")->find();
        $OrderDetails['itemdata'] = $itemdata;
        if ($itemdata['type'] == 2) {
            $OrderDetails['itemdata']['item'] = Item::where('id=' . $itemdata['pro_id'])->field('id,item_name,item_price,item_prices')->find();
        } elseif ($itemdata['type'] == 3) {
            $OrderDetails['itemdata']['vip'] = Vipcard::where('id=' . $itemdata['pro_id'])->field('id,vip_num,vip_name,vip_lines,vip_discount')->find();
        } elseif ($itemdata['type'] == 4) {
            $OrderDetails['itemdata']['experience'] = Expercard::where('id=' . $itemdata['pro_id'])->field('id,price')->find();
        }
        return $OrderDetails;
    }

}
